
// 通过wxPayment存储
export default class Payment {
    constructor({ effects }) {
        this.effects = effects;
    }

    *pay(payload) {
        // 创建预支付订单
        const res = yield this.effects.put.resolve({
            type: '$request/get',
            payload: {
                url: '/api/pay/wechat/pre/order',
                data: payload
            }
        });

        if (!res) {
            return false;
        }
        try {
            wx.requestPayment({
                timeStamp: res.timestamp,
                nonceStr: res.nonce_str,
                package: 'prepay_id=' + res.prepay_id,
                signType: 'HMAC-SHA256',
                paySign: res.paySign,
                success:() => {
                    this.effects.put({type: 'PAYMENT_SUCCESS'});
                }
            });

        } catch (e) {
            return false;
        }
    }
}
